#include <stdio.h>
#include <stdlib.h>

void find_closest_flight(int desired_time, int *departure_time, int *arrival_time);

int main(void) {
    int hour, minute, departure_time, arrival_time;
    char a_or_p_of_departure = 'a', a_or_p_of_arrival = 'a';

    printf("Enter a 24-hour time: ");
    scanf("%d :%d", &hour, &minute);

    find_closest_flight(hour * 60 + minute, &departure_time, &arrival_time);

    if (departure_time >= 12 * 60) a_or_p_of_departure = 'p';
    if (arrival_time >= 12 * 60) a_or_p_of_arrival = 'p';

    printf("Closest departure time is %.2d:%.2d %c.m. arriving at %.2d:%.2d %c.m.\n", departure_time / 60, departure_time % 60, a_or_p_of_departure,
        arrival_time / 60, arrival_time % 60, a_or_p_of_arrival);

    return 0;
}

void find_closest_flight(int desired_time, int *departure_time, int *arrival_time) {
    int departure_array[] = {
        8 * 60,
        9 * 60 + 43,
        11 * 60 + 19,
        12 * 60 + 47,
        14 * 60,
        15 * 60 + 45,
        19 * 60,
        21 * 60 + 45,
    };

    int arrival_array[] = {
        10 * 60 + 16,
        11 * 60 + 52,
        13 * 60 + 31,
        15 * 60,
        16 * 60 + 8,
        17 * 60 + 55,
        21 * 60 + 20,
        23 * 60 + 58,
    };

    int smallest_diff = abs(departure_time[0] - desired_time);
    for (int i = 1; i < 8; i++)
    {
        if (abs(departure_array[i] - desired_time) < smallest_diff) {
            smallest_diff = abs(departure_array[i] - desired_time);
            *departure_time = departure_array[i];
            *arrival_time = arrival_array[i];
        }
    }
}